var core    = require("core")
,   send    = core.send
,   db      = core.db
,   schema  = db.schema;

var GET = {
    path    : "/register",
    method  : "get",
    auth    : function(opt, next){
        if(opt.tokens.regAuth >= 0)
            return next();

        if(global.app.user.count === 0)
            next(new Error(new send(100018).msg));
        else next(true, "/login");

    }
};

var POST = {
    path    : "/register",
    method  : "post",
    auth    : [
        "Tokens.regAuth >= 0",
        "Body.name",
        "Body.pwd",
        function(opt, next){
            var query = {
                name : opt.body.name
            };
            if(global.app.user.count === 0)
                return next();

            schema.User.findOne(query, function(err, doc){
                if(err)
                    return next(err);

                if(doc)
                    return next(new Error(new send(100011).msg));

                next();
            });
        }
    ]
};

exports.GET = GET;
exports.POST = POST;
